/*
 * @(#)PaginationInjectionInterceptor.java
 *
 * Copyright 2010 Xinhua Online, Inc. All rights reserved.
 */

package com.winxuan.channel.support.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.winxuan.util.pagination.Pagination;
import com.winxuan.util.pagination.PaginationParser;


/**
 * description
 * @author  �
 * @version 1.0,Jun 22, 2010  12:18:31 PM
 */
public class PaginationInjectionInterceptor extends HandlerInterceptorAdapter {
	
	private static final String CURRENT_PAGE_TARGET = "pagination.currentPage";

	private static final String PAGE_SIZE_TARGET = "pagination.pageSize";

	private static final String SKIP_SIZE_TARGET = "pagination.skipSize";
	
	private static final String BEGIN_TARGET="pagination.begin";
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if(handler instanceof PaginationAware) {
			PaginationAware paginationAware = (PaginationAware) handler;
			PaginationParser paginationParser = new PaginationParser(request, 	response);
			paginationParser.setCurrentPageTarget(CURRENT_PAGE_TARGET);
			paginationParser.setPageSizeTarget(PAGE_SIZE_TARGET);
			paginationParser.setSkipSizeTarget(SKIP_SIZE_TARGET);
			paginationParser.setBeginTarget(BEGIN_TARGET);
			Pagination pagination = paginationParser.parse();
			paginationAware.setPagination(pagination);
		}
		return super.preHandle(request, response, handler);
	}
	
}

